<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>订单管理</title>
    <link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="css/comments.css" media="all">
    <style>
        .layui-form-label{
            width: 120px;
        }
        .show{
            display: none
        }
        #my_iframe{
            width: 100%;
            border: 0;
            height: 100%;
            display: none;
        }
        #modifyForm{
            display: none;
            padding: 20px 20px 0 0 ;
        }
        #copyPopup.layui-layer-content{
            overflow:unset!important;
        }
        #modifyForm .layui-input-block{
            margin-left: 150px;
        }
        .layui-form-select dl dd.layui-this{
            background-color: #ffffff;
            color: #333333;
        }
    </style>
</head>
<body>
    <div class="maincontainer">
        <blockquote class="layui-elem-quote show">
            <span class="block_title">订单管理</span>
            <button type="button" class="layui-btn layui-btn-sm" style="float: right;margin-top: -4px;" onclick="window.history.back();">返回</button>
        </blockquote>
        <div class="layui-card">
            <div class="layui-card-header layuiadmin-card-header-auto">
                <form id="queryForm" class="layui-form" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">订单编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="shopOrderNo" autocomplete="off" class="layui-input" placeholder="订单编号" maxlength="30">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">餐饮总部名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="foodManagerName" autocomplete="off" class="layui-input" placeholder="餐饮总部名称" maxlength="20">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">下单时间</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="selectTime" placeholder=" - ">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-inline">
                                <div id="orderStatus"></div>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">是否改单</label>
                            <div class="layui-input-inline">
                                <select name="updateOrderStatus" lay-filter="updateOrderStatus">
                                    <option value=""></option>
                                    <option value="1">是</option>
                                    <option value="0">否</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">商品名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="productName" autocomplete="off" class="layui-input" placeholder="商品名称" maxlength="30">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">食堂名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="canteenName" autocomplete="off" class="layui-input" placeholder="食堂名称" maxlength="30">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">交货日期</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="pickTimeSearch" placeholder=" - ">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn searchBth"  lay-submit lay-filter="searchBth">查询</button>
                            <button type="reset" class="layui-btn layui-btn-primary reReset" >重置</button>
                        </div>
                    </div>
                </form>
            </div>
            <div class="layui-card-body">
                <table class="layui-hide" id="selectTable" lay-filter="selectTable"></table>
            </div>
        </div>

    </div>
    <!-- <iframe id="my_iframe" src="" ></iframe> -->
    
    <div id="modifyForm">
        <form class="layui-form" action="" lay-filter="modifyForm">
            <div class="layui-form-item">
                <label class="layui-form-label">交货日期</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" id="pickTime" placeholder=" - ">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">订单状态</label>
                <div class="layui-input-block">
                    <select name="pickStatus" lay-filter="pickStatus">
                        <option value="">全部</option>
                        <!-- <option value="1">待提交</option> -->
                        <option value="4">待接单</option>
                        <!-- <option value="6">待确认</option> -->
                        <option value="5">待发货</option>
                        <option value="8">已发货</option>
                        <option value="9">已完成</option>
                        <option value="10">已取消</option>
                        <option value="3">待付款</option>
                    </select>
                </div>
            </div>
        </form>
    </div>
</body>

<script type="text/html" id="toolbarDemo">
	<div class="layui-btn-container toolbarDemo">
		<button class="layui-btn layui-btn-sm" lay-event="change">核准订单</button>
		<button class="layui-btn layui-btn-sm" lay-event="place">代下单</button>
		<!-- <button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="export">导出</button> -->
		<button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="exportAll">批量导出订单明细</button>
		<button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="pickExport">导出拣货单</button>
	</div>
</script>
<script type="text/javascript" src="plugins/layui/layui.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script src="plugins/layui/modules/xm-select.js"></script>
<script>
    layui.config({
        base: 'plugins/layui/modules/',
    }).use(['http', 'table', 'laydate'], function() {
        
        var form = layui.form,
            http = layui.http,
            table = layui.table,
            laydate = layui.laydate;
        var excelList,currPage,limit

		$('.reReset').click();
		
        // 重置
        window.onReset = function() {
            orderStatus.setValue([])
        }

        http.checking(function(supplierInfo) {

            var type = http.getPid('type')
            if(type){
                $('.show').show()
            }

            laydate.render({
                elem: '#selectTime'
                ,theme: '#fbaf7e'
                ,type: 'datetime'
                ,range: "-"
            });
            laydate.render({
                elem: '#pickTimeSearch'
                ,type: 'datetime'
                ,theme: '#fbaf7e'
                ,range: "-"
            });
            laydate.render({
                elem: '#pickTime'
                ,type: 'datetime'
                ,theme: '#fbaf7e'
                ,range: "-"
            });

            var search = sessionStorage.getItem('xi-can-supplier-orderMSearch')
            if(search){
                search = JSON.parse(search)
                currPage = search.page
                limit = search.limit
                
                $('#selectTime').val(search.selectTime)
                $('input[name=shopOrderNo]').val(search.shopOrderNo),
                $('input[name=foodManagerName]').val(search.foodManagerName),
                $('select[name=updateOrderStatus]').val(search.updateOrderStatus)
                $('input[name=productName]').val(search.productName)
                $('input[name=canteenName]').val(search.canteenName)
                $('#pickTimeSearch').val(search.pickTimeSearch)
                form.render('select')

                var option = []
                if(search.orderStatusSet){
                    option = search.orderStatusSet.split(',')
                }
                xmSelectFun(option); // 订单状态回显
            }else{
                xmSelectFun([]); // 订单状态回显
            }
            
            // 订单状态回显
            function xmSelectFun(option) {
                orderStatus = xmSelect.render({
                    el: '#orderStatus',
                    name: 'orderStatusSet',
                    style: {
                        borderColor: '#e6e6e6'
                    },
                    theme: {
                        color: '#f56643 ',
                    }, 
                    initValue: option,
                    data: [
                        {name: '待接单', value: 4},
                        {name: '待发货', value: 5},
                        {name: '已发货', value: 8},
                        {name: '已完成', value: 9},
                        {name: '已取消', value: 10},
                        {name: '待付款', value: 3},
                    ]
                })
                tableRender()
            }
            var selectTable;
            function tableRender(){
                selectTable = http.table({
                    elem: '#selectTable',
                    url: 'supplier/admin/shop/order/listPage',
                    method: 'get',
                    id: 'selectTable',
                    toolbar: '#toolbarDemo',
                    where: {
                        startTime: $('#selectTime').val()?$('#selectTime').val().split(' - ')[0]:'',
                        endTime: $('#selectTime').val()?$('#selectTime').val().split(' - ')[1]:'',
                        shopOrderNo: $('input[name=shopOrderNo]').val(),
                        foodManagerName: $('input[name=foodManagerName]').val(),
                        orderStatusSet: $('input[name=orderStatusSet]').val(),
                        updateOrderStatus: $('select[name=updateOrderStatus]').val(),
                        productName: $('input[name=productName]').val(),
                        canteenName: $('input[name=canteenName]').val(),
                        deliverStartTime: $('#pickTimeSearch').val()?$('#pickTimeSearch').val().split(' - ')[0]:'',
                        deliverEndTime: $('#pickTimeSearch').val()?$('#pickTimeSearch').val().split(' - ')[1]:'',
                    },
                    page: {
                        curr: currPage,
                        limit: limit
                    },
                    done: function(res, curr, count){
                        excelList = res.data;
                        currPage = curr
                        limit = selectTable.config.limit
                        getSearchVal() // 存储搜索条件  
                    },
                    cols: [[
				        {type:'checkbox',fixed: 'left'}
                        // {title:'序号',type:'numbers',fixed: 'left'}
                        ,{field:'shopOrderNo', title: '订单编号', align : 'center', width: 235,
                            templet: function(d){
                                var html = ''
                                html += '<a class="buttoncolor" lay-event="detail">'+d.shopOrderNo+'</a>'
                                return html
                            }
                        }
                        ,{field:'foodManagerName', title: '餐饮总部名称', align : 'center'}
                        ,{field:'canteenName', title: '食堂名称', align : 'center'}
                        ,{field:'orderTotalMoney', title: '金额', align : 'center'}
                        ,{field:'shelfStatus', title: '结账模式', align : 'center',
                            templet: function (d) {
                                return d.billType==1?'立结':'记账';
                            }
                        }
                        ,{field:'filterUpdateOrderStatus', title: '状态', align : 'center',
                            templet: function (d) {
                                var html = ''
                                switch(d.filterUpdateOrderStatus){
                                    // case 1:
                                    //     html = '<span style="color: #1ABC9C">待提交</span>'
                                    //     break
                                    case 3:
                                        html = '<span style="color: #FF0000">待付款</span>'
                                        break
                                    case 4:
                                        html = '<span style="color: #1ABC9C">待接单</span>'
                                        break
                                    case 5:
                                        html = '<span style="color: #0066FF">待发货</span>'
                                        break
                                    case 8:
                                        html = '<span style="color: #000000">已发货</span>'
                                        break
                                    case 9:
                                        html = '<span style="color: #000000">已完成</span>'
                                        break
                                    case 10:
                                        html = '<span style="color: #FF0000">已取消</span>'
                                        break
                                }
                                return html
                            }
                        }
                        ,{field:'updateStatus', title: '是否改单', align : 'center',
                            templet: function (d) {
                                return d.updateStatus==1?'是':'否';
                            }
                        }
                        ,{field:'createTime', title: '下单时间', align : 'center',width: 170}
                        ,{field:'', title: '操作', align : 'center',fixed: 'right',width: 100,
                            templet: function (d) {
                                var html = '';
                                if(supplierInfo.xicanSendStatus == 1){ // 是否鲜云链接单和发货 0是 1否
                                    if(d.filterUpdateOrderStatus==4){
                                        html += '<button type="button" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="order" >接单</button>'
                                    }
                                    if(d.filterUpdateOrderStatus==5){
                                        html += '<button type="button" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="send" >发货</button>'
                                    }
                                }
                                return html;
                            }
                        }
                    ]]
                });
            }
            // 获取查询数据
            function getSearchVal() {
                var val = {
                    page: currPage,
                    limit: limit,
                    selectTime: $('#selectTime').val(),
                    orderStatusSet: $('input[name=orderStatusSet]').val(),
                    shopOrderNo: $('input[name=shopOrderNo]').val(),
                    foodManagerName: $('input[name=foodManagerName]').val(),
                    updateOrderStatus: $('select[name=updateOrderStatus]').val(),
                    productName: $('input[name=productName]').val(),
                    canteenName: $('input[name=canteenName]').val(),
                    pickTimeSearch: $('#pickTimeSearch').val(),
                }
                
                sessionStorage.setItem('xi-can-supplier-orderMSearch', JSON.stringify(val))
            }

            // //查询
            form.on('submit(searchBth)', function (data) {
                let _data = data.field,
                    param = _data;
                var startTime = $('#selectTime').val()?$('#selectTime').val().split(' - ')[0]:'';
                var endTime = $('#selectTime').val()?$('#selectTime').val().split(' - ')[1]:'';
                var deliverStartTime = $('#pickTimeSearch').val()?$('#pickTimeSearch').val().split(' - ')[0]:'';
                var deliverEndTime = $('#pickTimeSearch').val()?$('#pickTimeSearch').val().split(' - ')[1]:'';
                param.startTime = startTime;
                param.endTime = endTime;
                param.deliverStartTime = deliverStartTime;
                param.deliverEndTime = deliverEndTime;

                selectTable.reload({
                    page:{curr:1},
                    where: param
                })
                return false;
            })

            table.on('tool(selectTable)', function(obj){
                var data = obj.data;
                if(obj.event=='order'){
                    // 接单
                    location.href = 'orderManagementDetail.html?id=' + data.shopOrderNo
                    // $('#my_iframe').attr('src', 'orderManagementDetail.html?id=' + data.shopOrderNo + '&type=1')
                    // $('.maincontainer').hide()
                    // $('#my_iframe').show()
                }else if(obj.event=='send'){
                    // 发货
                    location.href = 'orderManagementDetail.html?id=' + data.shopOrderNo
                    // $('#my_iframe').attr('src', 'orderManagementDetail.html?id=' + data.shopOrderNo + '&type=1')
                    // $('.maincontainer').hide()
                    // $('#my_iframe').show()
                    
                }else if(obj.event == 'detail'){
                    // 订单详情
                    location.href = "orderManagementDetail.html?id=" + data.shopOrderNo
                    // $('#my_iframe').attr('src', 'orderManagementDetail.html?id=' + data.shopOrderNo + '&type=1')
                    // $('.maincontainer').hide()
                    // $('#my_iframe').show()
                }
            })

            table.on('toolbar(selectTable)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
                if(obj.event=='change'){ 
                    // 核准订单
                    location.href = "orderManagementChange.html"
                }else if(obj.event=='export'){ // 导出
                    var data = checkStatus.data; //获取选中行数据
                    if (data.length == 0) {
                        layer.msg('当前页没有可导出的数据');
                        return;
                    }
                    
                    var param = getTheSearchVal()
                    if (param) {  
                        let paramsArray = [];  
                        //encodeURIComponent  
                        Object.keys(param).forEach(key => paramsArray.push(key + '=' + param[key]))  
                        window.open(TUrl+"supplier/admin/shop/order/export?"+paramsArray.join('&'))
                    }
                }else if(obj.event == 'exportAll') {
                    // 批量导出订单明细
                    var data = checkStatus.data; //获取选中行数据 
                    if (data.length == 0) {
                        layer.msg('当前页没有可导出的数据');
                        return;
                    }
                    let shopOrderNoArray = []
                    let authentication = localStorage.getItem('xi-can-supplier-authentication')
                    for(var i=0;i<data.length;i++){
                        shopOrderNoArray.push(data[i].shopOrderNo)
                    }
                    window.open(TUrl+"supplier/admin/shop/order/export/order/list/batch?authentication="+authentication+"&shopOrderNoArray="+shopOrderNoArray.toString())
                }else if(obj.event == 'place'){ // 代下单
                    location.href = 'orderManagementPlace.html'
                }else if(obj.event == 'pickExport'){
                    // 导出拣货单
                    // http.myOpen({
                    //     title:'请选择要导出的拣货单',
                    //     content: $('#modifyForm'),
                    //     area:['500px','250px'],
                    //     id: 'copyPopup',
                    //     btn:['确定','取消'],
                    //     btn1:function(index, layero){
                            var param = getTheSearchVal()
                            if (param) {  
                                let paramsArray = [];  
                                //encodeURIComponent  
                                Object.keys(param).forEach(key => paramsArray.push(key + '=' + param[key]))  
                                window.open(TUrl+"supplier/admin/shop/order/exportGoodOrder/v2?"+paramsArray.join('&'))
                            }
                    //     }
                    // })
                }
            })
            function getTheSearchVal() {
                var param = {
                    page: currPage,
                    limit: limit,
                    authentication: localStorage.getItem('xi-can-supplier-authentication'),
                    startTime: $('#selectTime').val()?$('#selectTime').val().split(' - ')[0]:'',
                    endTime: $('#selectTime').val()?$('#selectTime').val().split(' - ')[1]:'',
                    orderStatusSet: $('input[name=orderStatusSet]').val(),
                    shopOrderNo: $('input[name=shopOrderNo]').val(),
                    foodManagerName: $('input[name=foodManagerName]').val(),
                    updateOrderStatus: $('input[name=updateOrderStatus]').val(),
                    productName: $('input[name=productName]').val(),
                    canteenName: $('input[name=canteenName]').val(),
                    deliverStartTime: $('#pickTimeSearch').val()?$('#pickTimeSearch').val().split(' - ')[0]:'',
                    deliverEndTime: $('#pickTimeSearch').val()?$('#pickTimeSearch').val().split(' - ')[1]:'',
                }
                return param
            }
        })

    })

</script>
</html>